package com.af.financeomine.web.action;

import java.lang.reflect.InvocationTargetException;
import java.util.logging.Logger;

import org.apache.commons.beanutils.BeanUtils;
import org.vraptor.annotations.Component;
import org.vraptor.annotations.InterceptedBy;
import org.vraptor.annotations.Remotable;
import org.vraptor.plugin.hibernate.Validate;

import com.af.financeomine.dao.RegistroContaDao;
import com.af.financeomine.model.RegistroDespesaMensal;
import com.af.financeomine.web.flexigrid.FlexigridRows;
import com.af.financeomine.web.interceptor.LoginInterceptor;

@Component
@InterceptedBy(LoginInterceptor.class)
public class RegistroContaLogic {
	
	private static Logger log = Logger.getLogger(RegistroContaLogic.class.getName());

	private RegistroContaDao dao;
	private FlexigridRows rows;
	private int total;
	private int page = 1;
	
	public RegistroContaLogic(RegistroContaDao dao) {
		this.dao = dao;
	}
	
	@Validate(params="registroConta")
	public void save(RegistroDespesaMensal registroConta){
		if(registroConta.getId()==null || registroConta.getId() == 0){
			dao.save(registroConta);
		}else{
			RegistroDespesaMensal registro = dao.findById(registroConta.getId());
			try {
				BeanUtils.copyProperties(registro, registroConta);
			} catch (IllegalAccessException e) {
				log.warning("an error ocurred: "+ e.getMessage());
			} catch (InvocationTargetException e) {
				log.warning("an error ocurred: "+ e.getMessage());
			}
			dao.save(registro);
		}
	}
	
	@Remotable
	public void lista(Integer page,String qtype, Integer rp,String sortname,String sortorder, String query){
		
	}
	
	public void all(){
		
	}
	public FlexigridRows getRows() {
		return rows;
	}
	public int getTotal() {
		return total;
	}
	public int getPage() {
		return page;
	}
}
